import { PaginationResult } from "/@/api/models";
import {
  AdviceSetting,
  AlarmCountItem,
  AlarmGroupItem,
  AlarmSearchReq,
  CoutItem,
  NotifiDetailReq,
  NotifiItem,
  ReadAdviceItem,
  WorkOrderSearchReq,
} from "./models";
import req from "/@/api/req";

export enum Api {
  getNotifiPage = "api/msg/advice/company/page",
  add = "api/msg/advice/company/add",
  getById = "api/msg/advice/company/getById",
  adviceCount = "api/msg/advice/adviceCount",
  // 工单通知
  workOrderPage = "api/msg/advice/workOrder/page",
  workOrderClearUnread = "api/msg/advice/workOrder/clearUnread",
  // 平台通知
  addPlatformNotifi = "api/msg/advice/platform/add",
  platformPage = "api/msg/advice/platform/page",
  platformPublish = "api/msg/advice/platform/publish",
  platformClearUnread = "api/msg/advice/platform/clearUnread",
  platformUpdate = "api/msg/advice/platform/update",
  platformById = "api/msg/advice/platform/getById",
  platformDelete = "api/msg/advice/platform/delete",
  platformBatchDelete = "api/msg/advice/platform/batch/delete",
  // 企业通知
  companyClearUnread = "api/msg/advice/company/clearUnread",
  companyPage = "api/msg/advice/company/page",
  companyPublish = "api/msg/advice/company/publish",
  companyUpdate = "api/msg/advice/company/update",
  companyById = "api/msg/advice/company/getById",
  companyDelete = "api/msg/advice/company/delete",
  companyBatchDelete = "api/msg/advice/company/batch/delete",
  // 告警通知
  adviceAlarmCount = "api/msg/advice/adviceAlarmCount",
  getAlarmsGroupByProject = "api/v1/users/advice/alarm/getAlarmsGroupByProject",
  alarmClearUnread = "api/msg/advice/alarm/clearUnread",
  alarmPage = "api/v1/users/advice/alarm/page",
  getAlarmGroupList = "api/v1/users/advice/alarm",
  //通知已读
  readByAdviceId = "api/msg/advice/read/readByAdviceId",
  queryAdviceSettings = "api/msg/adviceSetting/queryAdviceSettings",
  updateAdviceSettings = "api/msg/adviceSetting/updateAdviceSettings",
}

// 平台清空未读
export function platformClearUnread(loading = true) {
  return req.post<boolean>(
    {
      url: Api.platformClearUnread,
      data: {},
    },
    {
      useGlobalLoading: loading,
    }
  );
}
// 企业清空未读
export function companyClearUnread(loading = true) {
  return req.post<boolean>(
    {
      url: Api.companyClearUnread,
      data: {},
    },
    {
      useGlobalLoading: loading,
    }
  );
}
// 告警清空未读
export function alarmClearUnread(loading = true) {
  return req.post<boolean>(
    {
      url: Api.alarmClearUnread,
      data: {},
    },
    {
      useGlobalLoading: loading,
    }
  );
}
// 工单清空未读
export function workOrderClearUnread(loading = true) {
  return req.post<boolean>(
    {
      url: Api.workOrderClearUnread,
      data: {},
    },
    {
      useGlobalLoading: loading,
    }
  );
}
export function getAlarmGroupList(type: "fault_type" | "project") {
  return req.post<AlarmGroupItem[]>({
    url: Api.getAlarmGroupList + "/" + type,
    data: {},
  });
}
export function addNotifi(data: NotifiItem) {
  return req.post<boolean>({
    url: Api.add,
    data,
  });
}
export function addPlatformNotifi(data: NotifiItem) {
  return req.post<boolean>({
    url: Api.addPlatformNotifi,
    data,
  });
}
export function companyUpdate(data: NotifiItem) {
  return req.post<boolean>({
    url: Api.companyUpdate,
    data,
  });
}
export function platformUpdate(data: NotifiItem) {
  return req.post<boolean>({
    url: Api.platformUpdate,
    data,
  });
}
export function getById(data: NotifiDetailReq) {
  return req.post<boolean>({
    url: Api.getById,
    data,
  });
}
export function adviceCount(data: {}) {
  return req.post<CoutItem>(
    {
      url: Api.adviceCount,
      data,
    },
    {
      useGlobalLoading: false,
    }
  );
}
export function workOrderPage(data: WorkOrderSearchReq) {
  return req.post<CoutItem>({
    url: Api.workOrderPage,
    data,
  });
}
// 告警通知统计接口
export function adviceAlarmCount(data: AlarmSearchReq) {
  return req.post<Array<AlarmCountItem>>({
    url: Api.adviceAlarmCount,
    params: data,
  });
}
// 告警通知统计接口
export function getAlarmsGroupByProject(data?: AlarmSearchReq) {
  return req.post<Array<AlarmCountItem>>({
    url: Api.getAlarmsGroupByProject,
    data: {},
  });
}
// 平台通知发布
export function platformPublish(data: NotifiDetailReq) {
  return req.post<boolean>({
    url: Api.platformPublish,
    params: data,
  });
}
// 企业通知发布
export function companyPublish(data: NotifiDetailReq) {
  return req.post<boolean>({
    url: Api.companyPublish,
    params: data,
  });
}

//获取排在前面的一些记录
export function getFrontData(url: string, size = 20) {
  return req.post<PaginationResult<NotifiItem>>(
    {
      url,
      data: {
        current: 1,
        size,
        model: {
          sendState: "SENT",
        },
        sort: "",
      },
    },
    {
      useGlobalLoading: false,
    }
  );
}
// 消除单条未读消息
export function readByAdviceId(data: ReadAdviceItem, useGlobalLoading = false) {
  return req.post<boolean>(
    {
      url: Api.readByAdviceId,
      data,
    },
    {
      useGlobalLoading,
    }
  );
}

// export function readByAdviceId(adviceId: string) {
// return req.commonPost(Api.readByAdviceId, { adviceId });
// }
// 平台消息详情
export function platformById(id: string) {
  return req.post<NotifiItem>({
    url: Api.platformById,
    params: { id },
  });
}
// 企业消息详情
export function companyById(id: string) {
  return req.post<NotifiItem>({
    url: Api.companyById,
    params: { id },
  });
}
// 删除平台通知
export function platformDelete(id: string) {
  return req.post<boolean>({
    url: Api.platformDelete,
    params: { id },
  });
}
// 删除企业通知
export function companyDelete(id: string) {
  return req.post<boolean>({
    url: Api.companyDelete,
    params: { id },
  });
}
// 批量删除平台通知
export function platformBatchDelete(data) {
  return req.post<boolean>({
    url: Api.platformBatchDelete,
    data,
  });
}
// 批量删除企业通知
export function companyBatchDelete(data) {
  return req.post<boolean>({
    url: Api.companyBatchDelete,
    data,
  });
}

export function queryAdviceSettings() {
  return req.commonPost<AdviceSetting>(Api.queryAdviceSettings, null, { useGlobalLoading: false });
}

export function updateAdviceSettings(data: AdviceSetting) {
  return req.commonPost<boolean>(Api.updateAdviceSettings, data, { useGlobalLoading: false });
}
